# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/10/28 16:54
desc: 公司搜索结果页
'''
import logging


from appium.webdriver.extensions.android.nativekey import AndroidKey
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support import expected_conditions as EC
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage
from proj_spec.CLUE.mobile_po.company_detail_page import CompanyDetailPage
from proj_spec.CLUE.mobile_po.company_list_page import CompanyListPage
from selenium.webdriver.common.keys import Keys


class SearchResultPage(CLUEAppBasePage):
    _search_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.search']/*[1]/*[1]/*[1]")
    _search_locator_ios = (AppiumBy.IOS_PREDICATE, 'type == "XCUIElementTypeSearchField"')
    _searching_status_locator_android = (AppiumBy.XPATH,"//*[contains(@text,'搜索中')]")
    #_searching_status_locator_ios = (AppiumBy.XPATH, "//*[contains(@text,'搜索中')]")


    def clear_search(self):
        # self.get_searchbox().clear()
        search_box = self.find_element(getattr(self, '_search_locator_' + self.os))
        search_box.clear()

    def get_search_result(self,text):
        # 从搜索结果列表中获取文本为text，且父级可点击的元素
        self._result_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.search']/parent::*/parent::*/parent::*/following-sibling::*/*[@text='%s']"%text)
        self._result_locator_ios = (AppiumBy.IOS_CLASS_CHAIN,'**/XCUIElementTypeStaticText[`label == "%s"`][2]'%text)

        search_result = self.find_element(getattr(self, '_result_locator_' + self.os))
        return search_result

    def get_search_results(self):
        """ 返回搜索结果的文本列表

        :return:
        """

        WebDriverWait(self.driver, 10).until(EC.invisibility_of_element_located(self._searching_status_locator))
        #page_source = self.driver.page_source
        search_results = self.find_elements(AppiumBy.XPATH, "//*[contains(@text,'pages/index/search')]/*[1]/*[1]/*")
        search_results = search_results[1:-1]
        try:
            return [ele.find_element(AppiumBy.XPATH,"//./child::*[2]").text for ele in search_results] # 从元素定位元素
        except Exception as e:
            logging.ERROR(e)
            return []


    def to_com_detail(self,company_name):
        """在搜索结果中选中company_name的结果，跳转到相应企业详情页面

        :param company_name: 公司名称

        :return:
        """
        result_company = self.get_search_result(company_name)
        result_company.click()
        return CompanyDetailPage(self.driver)


    def to_com_list(self):
        """ 跳转到企业清单页面
        前提：输入框已有输入内容

        :return:
        """
        self.find_element(getattr(self, '_search_locator_' + self.os)).click()
        if self.os=='android':
            self.driver.press_keycode(AndroidKey.ENTER)
        else:
            self.driver.switch_to.active_element.send_keys('\n')
        return CompanyListPage(self.driver)

    def to_search_page(self):
        """ 清空搜索框内容，回到搜索页面

        :return:
        """
        from proj_spec.CLUE.mobile_po.search_page import SearchPage #延迟导入，避免循环导入问题
        search_box = self.find_element(getattr(self, '_search_locator_' + self.os))
        search_box.clear()
        if self.os == 'ios':
            # 输入框内的数据过长时，clear无法完全清空，需要重复调用clear
            while search_box.get_attribute("value") is not None and len(search_box.get_attribute("value")) > 0:
                search_box.clear()
        return SearchPage(self.driver)

